package logic.ui.componentInfoMed
{
	import common.manager.GUI;
	import common.manager.ObserverEvt;
	
	import component.show.control.UISprite;
	import component.untils.ComponentItem;
	
	import fl.controls.TextInput;
	import fl.events.ComponentEvent;
	
	import flash.events.Event;
	
	import logic.def.Def;
	import logic.def.UIURL;

	public class TextInputInfoMed extends UISprite
	{
		public static const NAME : String = "TextInput";
		
		private var m_target:TextInput;
		private var m_tarComItem : ComponentItem;
		
		private var m_txtID : TextInput;
		private var m_txtX : TextInput;
		private var m_txtY : TextInput;
		private var m_txtWidth : TextInput;
		private var m_txtHeight : TextInput;
		
		private var m_txtHtmlText : TextInput;
		
		public function TextInputInfoMed()
		{
			super(NAME,UIURL["TextInput"]);
			
			GUI.add(NAME,this);
			GUI.setAlgin(NAME,GUI.ALIGN_RIGHT,GUI.ALIGN_TOP);
		}
		
		public override function onRegister():void
		{
			m_txtID = collection["txtId"];
			m_txtX = collection["Txt_X"];
			m_txtY = collection["Txt_Y"];
			m_txtWidth = collection["Txt_Width"];
			m_txtHeight = collection["Txt_Height"];
			m_txtHtmlText = collection["txtHtmlText"];
			
			m_txtID.removeEventListener(Event.CHANGE,onIdChanged);
			m_txtX.removeEventListener(ComponentEvent.ENTER,setX);
			m_txtY.removeEventListener(ComponentEvent.ENTER,setY);
			m_txtWidth.removeEventListener(ComponentEvent.ENTER,setWidth);
			m_txtHeight.removeEventListener(ComponentEvent.ENTER,setHeight);
			m_txtHtmlText.addEventListener(Event.CHANGE,onHtmlTxtChanged);
		}
		
		public override function destory() : void
		{
			m_txtID.removeEventListener(Event.CHANGE,onIdChanged);
			m_txtX.removeEventListener(ComponentEvent.ENTER,setX);
			m_txtY.removeEventListener(ComponentEvent.ENTER,setY);
			m_txtWidth.removeEventListener(ComponentEvent.ENTER,setWidth);
			m_txtHeight.removeEventListener(ComponentEvent.ENTER,setHeight);
			m_txtHtmlText.removeEventListener(Event.CHANGE,onHtmlTxtChanged);
			
			m_txtID = null;
			m_txtWidth = null;
			m_txtX = null;
			m_txtY = null;
			m_txtHeight = null;
			m_txtHtmlText = null;
		}
		
		public function get target() : ComponentItem
		{
			return m_tarComItem;
		}
		
		public function set target(value : ComponentItem) : void
		{
			m_target = value.item;
			m_tarComItem = value;
//			m_target.stage.focus = m_target;
			setTargetData();
		}
		
		private function setTargetData() : void
		{
			m_txtID.text = m_target.name;
			m_txtX.text = m_target.x.toString();
			m_txtY.text = m_target.y.toString();
			m_txtWidth.text = m_target.width.toString();
			m_txtHeight.text = m_target.height.toString();
			m_txtHtmlText.text = m_target.text;
		}
		
		private function onHtmlTxtChanged(evt : Event) : void
		{
			if(!m_target) return;
			
			m_target.htmlText = m_txtHtmlText.text;
			
			sendChangeMsg();
		}
		
		private function onIdChanged(evt : Event) : void
		{
			m_target.name = m_txtID.text;
			
			sendChangeMsg();
		}
		
		private function setX(evt : ComponentEvent) : void
		{
			m_target.x = Number(m_txtX.text);
			
			sendChangeMsg();
		}
		
		private function setY(evt : ComponentEvent) : void
		{
			m_target.y = Number(m_txtY.text);
			
			sendChangeMsg();
		}
		
		private function setWidth(evt : ComponentEvent) : void
		{
			m_target.width = Number(m_txtWidth.text);
			
			sendChangeMsg();
		}
		
		private function setHeight(evt : ComponentEvent) : void
		{
			m_target.height = Number(m_txtHeight.text);
			
			sendChangeMsg();
		}
		
		private function sendChangeMsg() : void
		{
			ObserverEvt.Send(Def.COMPONENTPROPERTYCHANGED);
		}
	}
}